package com.wbao.dianniu.manager;

import java.io.UnsupportedEncodingException;

import org.apache.http.entity.StringEntity;

import android.content.Context;

import com.wbao.dianniu.data.ActivityData;
import com.wbao.dianniu.http.Batch;
import com.wbao.dianniu.http.Command;
import com.wbao.dianniu.http.HttpUtil;
import com.wbao.dianniu.http.HttpUtil.MyResponse;
import com.wbao.dianniu.listener.ICreateActivity;
import com.wbao.dianniu.listener.ICreateActivityListener;
import com.wbao.dianniu.logical.Action;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.utils.JsonUtil;

/**
 * 创建活动管理类
 * 
 * @author yejianjun
 *
 */
public class CreateActivityManager implements ICreateActivity {

	private ICreateActivityListener mListener;
	private Context mContext;
	
	public CreateActivityManager(Context context) {
		mContext = context;
	}

	@Override
	public void apply(Integer id, int accountId, String subject,
			String startDate, String address, int maxPersion, int price,
			String content, String longitude, String latitude) {
		StringEntity stringEntity = null;
		try {
			Batch batch = new Batch();
			batch.setToken(GlobalContext.token);
			Command command = new Command();
			command.addAction(Action.ACTION_ACCOUNT_ACTIVITY_ADD)
					.addArgs("id", id)
					.addArgs("accountId", accountId)
					.addArgs("subject", subject)
					.addArgs("startDate", startDate)
					.addArgs("address", address)
					.addArgs("maxPersion", maxPersion)
					.addArgs("price", price)
					.addArgs("content", content)
					.addArgs("longitude", longitude)
					.addArgs("latitude", latitude);
			batch.addCommand(command);
			stringEntity = new StringEntity(batch.toJson(), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		HttpUtil http = new HttpUtil();
//		http.req(mContext, stringEntity, new myHandler(mContext));
		http.req(mContext, stringEntity, new MyResponse() {
			
			@Override
			public void responseSucess(Object obj) {
				// TODO Auto-generated method stub
				// 通知界面
				if (null != mListener)
					mListener.onCreateActivitySuccess();
			}
			
			@Override
			public void responseFailure(String msg) {
				// TODO Auto-generated method stub
				if (null != mListener)
					mListener.onCreateActivityFailure(0, msg);
			}
		});
	}

	@Override
	public boolean addCreateListener(ICreateActivityListener listener) {
		mListener = listener;
		return mListener == null ? false : true;
	}

	@Override
	public boolean removeCreateListener(ICreateActivityListener listener) {
		if (null == listener)
			return false;
		mListener = null;
		return true;
	}

}
